package com.zhang.buiness.amaster.workserver.service;


import com.zhang.buiness.amaster.workserver.dao.TGoodsDao;
import com.zhang.buiness.amaster.workserver.dao.UserDao;
import com.zhang.buiness.amaster.workserver.dao.entity.TGoods;
import com.zhang.buiness.amaster.workserver.dao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class WorkTransactionalServiceImpl {

    @Autowired
    private TGoodsDao tgoodsDao;
    @Autowired
    private UserDao userDao;

    @Transactional
    public String propagationRequired(String s) {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("ropagationRequired");
        tGoods.setCreateTime(new Date());
        tGoods.setName("ropagationRequired");
        tGoods.setPrice(1111);
        tgoodsDao.insert(tGoods);
        User user = new User();
        userDao.insert(null);
        return "ropagationRequired";
    }

    @Transactional
    public String propagationRequiredNestedCall (String s) {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("ropagationRequired");
        tGoods.setCreateTime(new Date());
        tGoods.setName("ropagationRequired");
        tGoods.setPrice(66666);
        tgoodsDao.insert(tGoods);
        this.propagationRequiredNestedCallExcute();
        return "propagationRequiredNestedCall";
    }

    public String propagationRequiredNestedCallExcute() {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("propagationRequiredNestedCallExcute");
        tGoods.setCreateTime(new Date());
        tGoods.setName("propagationRequiredNestedCallExcute");
        tGoods.setPrice(777777);
        tgoodsDao.insert(tGoods);
        User user = new User();
        userDao.insert(null);
        return "propagationRequiredNestedCallExcute";
    }


    @Transactional(propagation =Propagation.SUPPORTS)
    public String supportsNotTransactional(String s) {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("propagationsupports");
        tGoods.setCreateTime(new Date());
        tGoods.setName("propagationsupports");
        tGoods.setPrice(2222);
        tgoodsDao.insert(tGoods);
        User user = new User();
        userDao.insert(null);
        return "test/supports";

    }

    @Transactional(propagation =Propagation.SUPPORTS)
    public String propagationsupports(String s) {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("propagationsupports");
        tGoods.setCreateTime(new Date());
        tGoods.setName("propagationsupports");
        tGoods.setPrice(2222);
        tgoodsDao.insert(tGoods);
        User user = new User();
        userDao.insert(null);
        return "test/supports";

    }

    @Transactional
    public String supportsHaveTransactional(String s) {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("propagationsupportsHave");
        tGoods.setCreateTime(new Date());
        tGoods.setName("propagationsupportsHave");
        tGoods.setPrice(3333);
        tgoodsDao.insert(tGoods);
        this.supportsHaveTransactionalExcute();
        return "test/supports";
    }

    @Transactional(propagation =Propagation.SUPPORTS)
    public void supportsHaveTransactionalExcute() {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("supportsHaveTransactional---Excute");
        tGoods.setCreateTime(new Date());
        tGoods.setName("supportsHaveTransactional--Excute");
        tGoods.setPrice(44444);
        tgoodsDao.insert(tGoods);
        User user = new User();
        userDao.insert(null);
    }


    @Transactional(propagation=Propagation.MANDATORY)
    public String mandatoryTransactional(String s) {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("mandatoryTransactional");
        tGoods.setCreateTime(new Date());
        tGoods.setName("mandatoryTransactional");
        tGoods.setPrice(5555);
        tgoodsDao.insert(tGoods);
        User user = new User();
        user.setCreateTime(new Date());
        user.setUsername("lisi");
        user.setMobile("21312321321");
        userDao.insert(user);
        return "mandatoryTransactional";
    }

    @Transactional
    public String mandatoryTransactionalNestedCall(String s) {
        this.mandatoryTransactionalNestedCallB();
        return "mandatoryTransactionalNestedCall";
    }
    @Transactional(propagation=Propagation.MANDATORY)
    public void mandatoryTransactionalNestedCallB() {
        this.mandatoryTransactionalNestedCallC();
    }

    public void mandatoryTransactionalNestedCallC() {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("mandatoryTransactionalNestedCallC");
        tGoods.setCreateTime(new Date());
        tGoods.setName("mandatoryTransactionalNestedCallC");
        tGoods.setPrice(888);
        tgoodsDao.insert(tGoods);
        User user = new User();
        userDao.insert(null);
    }

    @Transactional
    public String notsupportsTransactional(String s) {
        this.notsupportsTransactionalExcute();
        return "notsupportsTransactionalExcute";
    }

    @Transactional(propagation=Propagation.NOT_SUPPORTED)
    public void notsupportsTransactionalExcute() {
        TGoods tGoods = new TGoods();
        tGoods.setDescription("notsupportsTransactionalExcute");
        tGoods.setCreateTime(new Date());
        tGoods.setName("notsupportsTransactionalExcute");
        tGoods.setPrice(9999);
        tgoodsDao.insert(tGoods);
    }
}
